package com.mla.fastdfs.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mla.fastdfs.commom.Result;
import com.mla.fastdfs.dto.PermissionDto;
import com.mla.fastdfs.mapper.PermissionMapper;
import com.mla.fastdfs.mapper.RolePermissionMapper;
import com.mla.fastdfs.pojo.Permission;
import com.mla.fastdfs.service.PermissionService;
import com.mla.fastdfs.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Jun
 * @version v1.0
 * @date 2020-08-17
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;


    public void insert(Permission record) {
        record.setInsertTime(DateUtil.getStrTime());
        record.setUpdateTime(DateUtil.getStrTime());
        permissionMapper.insert(record);
    }


    public void update(Permission record) {
        record.setUpdateTime(DateUtil.getStrTime());
        permissionMapper.updateById(record);
    }


    public void del(Integer id) {
        //删除当前权限
        permissionMapper.deleteById(id);
        rolePermissionMapper.deleteById(id);
        //默认向下删除两级子权限
        List<Permission> list = permissionMapper.selectByRecord(new Permission().setPid(id));
        for (Permission permission : list) {
            permissionMapper.deleteById(permission.getId());
            rolePermissionMapper.deleteById(permission.getId());
            List<Permission> list1 = permissionMapper.selectByRecord(new Permission().setPid(permission.getPid()));
            for (Permission permission1 : list1) {
                permissionMapper.deleteById(permission1.getId());
                rolePermissionMapper.deleteById(permission1.getId());
            }
        }

    }


    public Permission selectByPrimaryKey(Integer id) {
        return permissionMapper.selectById(id);
    }


    public List<Permission> getList(Permission record) {
        return permissionMapper.selectByRecord(record);
    }


    public PageInfo<Permission> pageList(Permission record, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Permission> list = permissionMapper.selectByRecord(record);
        return new PageInfo<>(list);
    }

    public Result savePerm(Permission record) {
        if (record.getIstype() == 1) {
            List<Permission> repeat = permissionMapper.checkRepeat(record);
            if (repeat.size() > 0) {
                return Result.error("权限名称、权限路径、权限编号存在重复");
            }
        }
        int index = 1;
        if (record.getPid() != null && record.getPid() != 0) {
            Permission permission = permissionMapper.selectById(record.getPid());
            if (permission != null) {
                if (permission.getLevel() > 3) {
                    return Result.error("不支持更低等级的创建");
                }
                index = permission.getLevel() + index;
            }
        }
        record.setLevel(index);
        if (record.getId() == null) {
            insert(record);
        } else {
            update(record);
        }
        return Result.ok();
    }

    public Result getMenu(String userId) {
        List<Permission> permissions = permissionMapper.getUserMenu(userId);
        return Result.ok(permissions);
    }

    public List<PermissionDto> treeList(Permission record) {
        List<PermissionDto> treeList = permissionMapper.treeList(record);
        return treeList;
    }
}